package com.bishe.tax.service;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FileUploadService {
	private String filePath = "D:/TAXIMG/"; // 定义上传文件的存放位置

	public String upLoadFile(MultipartFile upload) throws Exception {
		String suffix = getFileSuffix(upload.getOriginalFilename()); // 获取上传文件的名字
		// 判断文件夹是否存在,不存在则创建
		File file = new File(filePath);
		if (!file.exists()) {
			file.mkdirs();
		}
		String newFileName = UUID.randomUUID().toString() + "." + suffix;
		String newFilePath = filePath + newFileName; // 新文件的路径
		upload.transferTo(new File(newFilePath)); // 将传来的文件写入新建的文件
		return "getImg/no?imgFile=" + newFileName;
	}

	/** 返回文件的后缀 **/
	private String getFileSuffix(String fileName) {
		String Suffix = "";
		if (fileName != null) {
			String[] str = fileName.split("\\.");
			if (str.length > 0) {
				Suffix = str[str.length - 1];
			}
		}
		return Suffix;
	}
}
